package org.ielts.service;

import java.util.List;

import org.ielts.domain.security.User;
import org.ielts.repository.security.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TeacherService {

	@Autowired
	UserRepository userRepository;

	String role = "教师";

	@Transactional
	public Iterable<User> findAll() {

		Iterable<User> teachers = userRepository.findByRoleName(role);

		return teachers;
	}

	@Transactional
	public User findOne(String teacherId) {
		User user = userRepository.findOne(teacherId);
		return user;
	}

	@Transactional
	public User findByName(String name) {
		User user = userRepository.findByNameAndRole(name, role);

		return user;
	}

	public User findByNameOrLoginNameOrNo(String n) {
		Iterable<User> users = userRepository.findByName(n);
		List<User> list = (List<User>) users;
		if (list.size() == 1) {
			return list.get(0);
		}
		User user = userRepository.findByLoginName(n);
		if (user != null) {
			return user;
		}

		user = userRepository.findByNo(n);
		if (user != null) {
			return user;
		}

		return null;
	}
}
